﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dhgms.TplHelper.Controller.Task.Sql
{
    using System.Data.Common;
    using System.Reflection;

    using Dhgms.DataManager.Model.Helper.Database;
    using Dhgms.TplHelper.Model.Info.TaskResult;

    /// <summary>
    /// Task for pulling an SQL query into a stongly typed collection
    /// </summary>
    /// <typeparam name="TDatabaseHelperClass">
    /// The type for the database helper
    /// </typeparam>
    /// <typeparam name="TInformationClass">
    /// The type for the information class
    /// </typeparam>
    /// <typeparam name="TParameterClass">
    /// The type for the SQL Parameters
    /// </typeparam>
    /// <typeparam name="TAdoNetHelperClass">
    /// Type for the ADO.NET helper class
    /// </typeparam>
    public abstract class StronglyTypedCollection<TDatabaseHelperClass, TInformationClass, TParameterClass, TAdoNetHelperClass> :
        Base<StronglyTypedCollection<TInformationClass>, TDatabaseHelperClass, TParameterClass>
        where TDatabaseHelperClass : DatabaseHelperBase<TParameterClass>, new()
        where TInformationClass : DataManager.Model.Info.Base<TInformationClass>, new()
        where TParameterClass : DbParameter
        where TAdoNetHelperClass : DataManager.Model.Helper.AdoNet.Base<TInformationClass>, new()
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="StronglyTypedCollection{TDatabaseHelperClass,TInformationClass,TParameterClass,TAdoNetHelperClass}"/> class.
        /// </summary>
        /// <param name="taskName">
        /// The task name.
        /// </param>
        /// <param name="connectionString">
        /// The connection string.
        /// </param>
        protected StronglyTypedCollection(string taskName, string connectionString)
            : base(taskName, connectionString)
        {
        }

        /// <summary>
        /// The OnProcess event for doing the donkey work
        /// </summary>
        /// <param name="result">
        /// object that contains result information
        /// </param>
        protected override void OnProcess(StronglyTypedCollection<TInformationClass> result)
        {
            result.Result = this.DbHelper.GetStronglyTypedCollection<TInformationClass, TAdoNetHelperClass>(
                this.ConnectionString, this.GetSql(), this.GetParameters(), false);
        }
    }
}
